package com.codrim.cs.data.server.rpc;

import com.alibaba.dubbo.config.annotation.Service;
import com.codrim.common.utils.bean.BeanUtils;
import com.codrim.common.utils.exception.Assert;
import com.codrim.common.utils.json.JsonMapper;
import com.codrim.cs.data.rpc.CooperatorsStatService;
import com.codrim.cs.data.rpc.dto.param.CooperatorsStatParamDTO;
import com.codrim.cs.data.rpc.dto.result.CooperatorsStatResultDTO;
import com.codrim.cs.data.server.dao.entity.CooperatorsStatDO;
import com.codrim.cs.data.server.dao.mapper.StatCsAdvAffMapper;
import com.codrim.cs.data.server.dao.param.CooperatorsDayStatParam;
import com.codrim.cs.data.server.subservice.TimeZoneSubService;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

/**
 * @author Liang.Zhuge
 * @date 04/05/2017
 */
@Service(version = CooperatorsStatService.RPC_VERSION)
public class CooperatorsStatServiceImpl implements CooperatorsStatService {
    private static final Logger logger = LoggerFactory.getLogger(CooperatorsStatServiceImpl.class);

    @Resource StatCsAdvAffMapper statCsAdvAffMapper;
    @Resource TimeZoneSubService timeZoneSubService;

    @Override
    public List<CooperatorsStatResultDTO> inCooperateAdvertiserStatData(CooperatorsStatParamDTO param) {
        logger.info("Cooperators stat, param:{}", JsonMapper.getInstance().toJson(param));

        assertParam(param);

        List<CooperatorsStatDO> cooperatorsStatDOs = statCsAdvAffMapper.cooperatorsAdvertiserStatData(
                getCooperatorsDayStatParam(param), timeZoneSubService.prefixTableNameForTimeZone(param.getTimeZone()));
        if (CollectionUtils.isEmpty(cooperatorsStatDOs)) {
            return null;
        }
        return cooperatorsStatDOs.stream().map(this::toCooperatorsStatResultDTO).collect(Collectors.toList());
    }

    private CooperatorsStatResultDTO toCooperatorsStatResultDTO(CooperatorsStatDO item) {
        return BeanUtils.copyProperties(item, CooperatorsStatResultDTO.class);
    }

    private CooperatorsDayStatParam getCooperatorsDayStatParam(CooperatorsStatParamDTO param) {
        CooperatorsDayStatParam cooperatorsStatParam = new CooperatorsDayStatParam();
        cooperatorsStatParam.setZone(param.getTimeZone());
        cooperatorsStatParam.setStartDate(param.getStartDate());
        cooperatorsStatParam.setEndDate(param.getEndDate());
        cooperatorsStatParam.setCompanyId(param.getCompanyId());
        cooperatorsStatParam.setCooperatorIds(param.getCooperatorsId());
        return cooperatorsStatParam;
    }

    private void assertParam(CooperatorsStatParamDTO param) {
        Assert.notNull(param.getTimeZone(), "TimeZone can not be null");
        Assert.notNull(param.getCompanyId(), "CompanyId can not be null");
        Assert.notNull(param.getStartDate(), "Start date can not be null");
    }

    @Override
    public List<CooperatorsStatResultDTO> inCooperateAffiliateStatData(CooperatorsStatParamDTO param) {
        assertParam(param);

        List<CooperatorsStatDO> cooperatorsStatDOs = statCsAdvAffMapper.cooperatorsAffiliateStatData(
                getCooperatorsDayStatParam(param), timeZoneSubService.prefixTableNameForTimeZone(param.getTimeZone()));
        if (CollectionUtils.isEmpty(cooperatorsStatDOs)) {
            return null;
        }
        return cooperatorsStatDOs.stream().map(this::toCooperatorsStatResultDTO).collect(Collectors.toList());
    }

}
